
let fn = {
  getCmdMac(cmdStr) {
    let str = cmdStr.slice(6, 18).toLocaleUpperCase()

    return fn.toHexArr(str).reverse().join(':')
  },
  toHexArr(str) {
    let tmpArr = []
    for (let i = 0; i < str.length; i += 2) {
      tmpArr.push(`${str[i]}${str[i + 1]}`)
    }

    return tmpArr
  },
  sleep(time = 1) {
    return new Promise(resolve => {
      setTimeout(() => {
        resolve()
      }, time)
    })
  },
  arrayToHexString(data) {
    let str = "";

    data.forEach((item) => {
      const s = item.toString(16)
      if (s.length > 1) {
        str += s
      } else {
        str += '0' + s
      }
    })
    return str
  },
  isHeartRateScale(scaleType) {
    return scaleType === 0x21
  },
  decodeWeight(a1, b1, weightScale = 100) {
    const d = (a1 << 8) + b1
    let weight = d / weightScale

    while (weight > 250) {
      weight = weight / 10
    }
    return weight
  },
  buildCmd(cmd, deviceType, ...data) {
    const cmdData = [cmd, data.length + 4, deviceType]
    let checksum = 0
    cmdData.push(...data)
    cmdData.forEach((item) => checksum += item)
    checksum = checksum & 0xFF
    cmdData.push(checksum)

    console.log("写入的命令", cmdData)
    return cmdData;

  }
}

module.exports = fn